package com.chigga.admin.controller;

import com.chigga.admin.domain.dto.UserCreateDTO;
import com.chigga.admin.domain.dto.UserPageQueryDTO;
import com.chigga.admin.domain.dto.UserQueryDTO;
import com.chigga.admin.domain.dto.UserUpdateDTO;
import com.chigga.admin.domain.vo.UserVO;
import com.chigga.admin.service.UserService;
import com.chigga.common.core.controller.BaseController;
import com.chigga.common.core.domain.PageResult;
import com.chigga.common.core.domain.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口
 *
 * @author DefChigga
 */
@Validated // 开启 validation 参数校验
@Tag(name = "用户管理")
@RequestMapping("/user")
@RestController
@Slf4j
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    /**
     * 创建用户
     *
     * @param userCreateDTO 创建用户信息
     */
    @Operation(summary = "创建用户")
    @PostMapping
    public ResponseResult<Integer> createUser(@Valid @RequestBody UserCreateDTO userCreateDTO) {
        log.info("创建用户：{}", userCreateDTO);
        return createResponseResult(userService.createUser(userCreateDTO));
    }

    /**
     * 更新用户
     *
     * @param userUpdateDTO 更新用户信息
     */
    @Operation(summary = "更新用户")
    @PutMapping
    public ResponseResult<Integer> updateUser(@Valid @RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("更新用户：{}", userUpdateDTO);
        return updateResponseResult(userService.updateUser(userUpdateDTO));
    }

    /**
     * 根据ID删除用户
     *
     * @param id 用户ID
     */
    @Operation(summary = "根据ID删除用户")
    @Parameter(name = "id", description = "用户ID", required = true, example = "1")
    @DeleteMapping("/{id}")
    public ResponseResult<Integer> deleteUserById(@Positive(message = "主键ID必须为正数") @PathVariable Long id) {
        log.info("删除用户：{}", id);
        return deleteResponseResult(userService.deleteUserById(id));
    }

    /**
     * 批量删除用户
     *
     * @param ids 用户ID列表
     */
    @Operation(summary = "批量删除用户")
    @Parameter(name = "ids", description = "用户ID列表，多个ID用逗号分隔", required = true, example = "1,2,3")
    @DeleteMapping("/batch")
    public ResponseResult<Integer> deleteUserBatchIds(@NotEmpty(message = "用户ID列表不能为空") @RequestParam List<Long> ids) {
        log.info("批量删除用户：{}", ids);
        return deleteResponseResult(userService.deleteUserBatchIds(ids));
    }

    /**
     * 查询用户列表
     *
     * @param userQueryDTO 查询条件
     * @return 用户列表
     */
    @Operation(summary = "查询用户列表")
    @GetMapping(value = "/list")
    public ResponseResult<List<UserVO>> selectUserList(@Valid @ModelAttribute UserQueryDTO userQueryDTO) {
        log.info("查询用户列表：{}", userQueryDTO);
        return selectResponseResult(userService.selectUserList(userQueryDTO));
    }

    /**
     * 分页查询用户列表
     * @param pageQuery 分页查询条件
     *
     * @return 分页用户列表
     */
    @Operation(summary = "分页查询用户列表")
    @PostMapping("/page")
    public ResponseResult<PageResult<UserVO>> selectUserPage(@Valid @RequestBody UserPageQueryDTO pageQuery) {
        log.info("分页查询用户列表：{}", pageQuery);
        return selectResponseResult(userService.selectUserPage(pageQuery));
    }

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return UserVO
     */
    @Operation(summary = "根据ID查询用户")
    @GetMapping("/{id}")
    public ResponseResult<UserVO> selectUserById(@Positive(message = "主键ID必须为正数") @PathVariable Long id) {
        return selectResponseResult(userService.selectUserById(id));
    }
}
